/*
 * Copyright (C) 2015 AChep@xda <artemchep@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */
package com.achep.base.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import com.achep.base.Device;

import java.util.IllegalFormatException;
import java.util.Locale;

/**
 * Created by Artem Chepurnoy on 20.02.2015.
 */
public class ResUtils {

    /**
     * @see #getString(android.content.res.Resources, int, Object...)
     */
    @NonNull
    public static String getString(@NonNull Context context, @StringRes int id, Object... formatArgs) {
        return getString(context.getResources(), id, formatArgs);
    }

    /**
     * Return the string value associated with a particular resource ID,
     * substituting the format arguments as defined in {@link java.util.Formatter}
     * and {@link java.lang.String#format}. It will be stripped of any styled text
     * information.
     *
     * @param id         The desired resource identifier, as generated by the aapt
     *                   tool. This integer encodes the package, type, and resource
     *                   entry. The value 0 is an invalid identifier.
     * @param formatArgs The format arguments that will be used for substitution.
     * @return String The string data associated with the resource,
     * stripped of styled text information.
     * @throws Resources.NotFoundException Throws NotFoundException if the given ID does not exist.
     */
    @NonNull
    public static String getString(@NonNull Resources resources, @StringRes int id, Object... formatArgs) {
        try {
            return resources.getString(id, formatArgs);
        } catch (IllegalFormatException e) {
            final String message = "Failed to format the string resource! The user locale is:"
                    + Locale.getDefault().toString();
            throw new IllegalArgumentException(message, e);
        }
    }

    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    @Nullable
    public static Drawable getDrawable(@NonNull Context context, @DrawableRes int drawableRes) {
        return Device.hasLollipopApi()
                ? context.getResources().getDrawable(drawableRes, context.getTheme())
                : context.getResources().getDrawable(drawableRes);
    }

}
